package com.zhiche.lisa.lspm.service.carrier.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.enums.LicenseTypeEnum;
import com.zhiche.lisa.lspm.dao.mapper.carrier.CarrierLicenseAttachMapper;
import com.zhiche.lisa.lspm.dao.mapper.carrier.CarrierLicenseMapper;
import com.zhiche.lisa.lspm.dao.model.carrier.CarrierLicense;
import com.zhiche.lisa.lspm.dao.model.carrier.CarrierLicenseAttach;
import com.zhiche.lisa.lspm.dao.model.driver.DriverLicense;
import com.zhiche.lisa.lspm.service.carrier.ICarrierLicenseService;
import com.zhiche.lisa.lspm.service.upload.UploadService;
import com.zhiche.lisa.lspm.service.utils.DevopUtil;
import com.zhiche.lisa.lspm.service.utils.MdmUtil;
import com.zhiche.lisa.lspm.vo.common.DrivingLicenseVO;
import com.zhiche.lisa.lspm.vo.common.LicenseTypeVo;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 牵引车证照 service impl
 *
 * @author lbl
 * @since 2018-05-25
 */
@Service
public class CarrierLicenseServiceImpl extends ServiceImpl<CarrierLicenseMapper, CarrierLicense> implements ICarrierLicenseService {

    @Autowired
    private CarrierLicenseMapper carrierLicenseMapper;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private MdmUtil mdmUtil;

    @Autowired
    private DevopUtil devopUtil;

    @Override
    public void updateLicense(String key, Long id, String authorization) {
        new Thread(() -> {
            String downloadUrl = uploadService.getStrUrl(key);
            //加入识别信息

            CarrierLicense carrierLicense = carrierLicenseMapper.selectById(id);
            Long licenseTypeId = carrierLicense.getLicenseTypeId();
            LicenseTypeVo licenseTypeVo = mdmUtil.getLicenseTypeById(licenseTypeId.toString());
            DrivingLicenseVO drivingLicenseVO;
            if (Objects.nonNull(licenseTypeVo)) {
                //行驶证
                if (licenseTypeVo.getTypeCode().equals(LicenseTypeEnum.VEHICLE_VEHICLE_LICENSE.getCode())) {
                    drivingLicenseVO = devopUtil.getDrivingLicenseVO(downloadUrl, authorization);
                    if (Objects.nonNull(drivingLicenseVO)) {
                        if (StringUtils.isNotEmpty(drivingLicenseVO.getVin()))
                            carrierLicense.setLicenseId(drivingLicenseVO.getVin());
                        if (StringUtils.isNotEmpty(drivingLicenseVO.getRegdate()))
                            carrierLicense.setGmtReg(DateTime.parse(drivingLicenseVO.getRegdate(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate());
                        if (StringUtils.isNotEmpty(drivingLicenseVO.getGmtOverdue()))
                            carrierLicense.setGmtOverdue(DateTime.parse(drivingLicenseVO.getGmtOverdue(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate());
                        this.updateById(carrierLicense);
                    }
                }
            }
        }).start();
    }

    @Override
    public List<CarrierLicense> selectListNoTenant(EntityWrapper<CarrierLicense> clEW) {
        return baseMapper.selectListNoTenant(clEW);
    }

}
